(function(){
	$.extend($.fn,{
		goodsClassifyComboBox: function() {
			var $that_ = $(this);
			var options_ = {
				deep: 0,// 最多加载到的根一级级别
				value: 0,// 默认值
				isParentValue: false,// value值是否为上级类ID
				isLoadParent: true,// 是否加载上级列表
				isShowNull: true,// 是否显示null项
				nullName: false,// null选项显示名
				onLoadParentBefore: function(parentId) {return true;},// 上级列表加载前
				onLoad: function($select){},// 列表加载显示后
				onSelect: function(deep, category){},// 选择后
				companyId: 0
			};
			var cacheData_ = {};
			var loadRootsPath_ = g_domain + "/goodsClassify/listRoots/json";
			var loadSonsPath_ = g_domain + "/goodsClassify/listSons/json";
			var loadBrothersPath_ = g_domain + "/goodsClassify/listBrothers/json";
			var containerId_ = "selectClassify_" + $that_.attr("id") + "_";	
			var loadBrothers_ = function(classifyId, isPrepend){
				var vars = {
					"classifyId":classifyId
				};
				
				var cachekey = $.param({path: loadBrothersPath_, vars: vars});
				var cacheData = storage_(cachekey);
				if (cacheData) {
					makeSelectHtml_($.parseJSON(cacheData), classifyId, isPrepend);
					return;
				} 
				
				$.ajax({
					url: loadBrothersPath_ + "?r=" + Math.random(),
					data: vars,
					dataType: 'text',
					async: false,
					success: function(data, textStatus, jqXHR) {
						storage_(cachekey, data);
						makeSelectHtml_($.parseJSON(data), classifyId, isPrepend);
					}
				});
			};
			var loadSons_ = function(classifyId, value){
				var loadPath = loadRootsPath_;
				var vars = {
					"companyId": options_.companyId
				};
				if (classifyId) {
					loadPath = loadSonsPath_;
					vars = {
						"classifyId":classifyId
					};
				}
				
				var cachekey = $.param({path: loadPath, vars: vars});
				var cacheData = storage_(cachekey);
				if (cacheData) {
					makeSelectHtml_($.parseJSON(cacheData), value);
					return;
				} 
				
				$.ajax({
					url: loadPath + "?r=" + Math.random(),
					data: vars,
					dataType: 'text',
					async: false,
					success: function(data, textStatus, jqXHR) {
						storage_(cachekey, data);
						makeSelectHtml_($.parseJSON(data), value);
					}
				});
			};
			var makeSelectHtml_  = function(data, value, isPrepend) {
				if (data.status !== 0) {
					alert(data.message);
					return;
				}
				var parent = data.result;
				if (!parent) {
					return;
				}
				var list = parent.goodsClassifies;
				if (!list || list.length === 0) {
					return;
				}
				var deep = parseInt(list[0].deep);
				var selectTagId = containerId_ + deep;
				var $select = $("#" + selectTagId);
				if ($select.length === 0) {
					if (isPrepend) {
						$that_.prepend('<select id="' + selectTagId + '"></select>');
					} else {
						$that_.append('<select id="' + selectTagId + '"></select>');
					}
					$select = $("#" + selectTagId);
					
				} else {
					$select.html("");
				}
				
				if (options_.isShowNull) {
					var nullName = options_.nullName?options_.nullName:"-" + (deep + 1) + "级类目-";
					$select.append("<option value=''>" + nullName + "</option>");
					
				}
				for (var i = 0; i < list.length; i++) {
					cacheData_[list[i].classifyId] = list[i];
					$select.append("<option value='" + list[i].classifyId + "'>" + list[i].name + "</option>");
				}
				
				// 当前列表有选中值,但下一级没有加载,则加载下一级
				if (value && $("#" + containerId_ + (deep + 1)).length === 0) {
					loadSons_(value);
				}
				
				if (options_.isLoadParent && parent && parent.deep >= options_.deep && $("#" + containerId_ + (deep - 1)).length === 0) {
					if (!options_.onLoadParentBefore || options_.onLoadParentBefore(parent.classifyId)) {
						loadBrothers_(parent.classifyId, true);
					}
				}
				// 选中值
				$select.val(value);
				options_.onSelect(deep, cacheData_[value]);
				options_.onLoad($select);
			};
			
			var storage_ = function(key, value) {
				if (!sessionStorage) {
					return null;
				}
				if (value) {
					return sessionStorage.setItem(key, value);
				} else {
					return sessionStorage.getItem(key);
				}
			};
			
			if (arguments.length > 0) {
				var options = arguments[0];
				for (var key in options) {
					if (options_[key] !== undefined) {
						options_[key] = options[key];
					}
				}
			}
			
			$(document).off("change", "[id^='" + containerId_ + "']");
			$(document).on("change", "[id^='" + containerId_ + "']", function() {
				var value = $(this).val();
				var idSplits = $(this).attr("id").split("_");
				var deep = parseInt(idSplits[idSplits.length -1]);
				$("[id^='" + containerId_ + "']").each(function(i, el) {
					idSplits = $(this).attr("id").split("_");
					var deep2 = parseInt(idSplits[idSplits.length -1]);
					if (deep2 > deep) {
						options_.onSelect(deep2);
						$(this).remove();
					}
				});
				
				options_.onSelect(deep, cacheData_[value]);
				
				if (value) {
					loadSons_(value);
				}
			});
			
			setTimeout(function() {
				if (options_.value) {
					if (options_.isParentValue) {
						loadSons_(options_.value);
					} else {
						loadBrothers_(options_.value);
					}
				} else {
					loadSons_();
				}
			});
			
		}
	});
})();